﻿using B.S.Database.Domain.Entity;
using B.S.Database.ErrorCode;
using B.S.Database.Interface.Interface;
using B.S.Database.Read.Api.Application.Commands.Traceability;
using B.S.Database.Read.Api.Dtos.RequsetDtos;
using MediatR;

namespace B.S.Database.Read.Api.Application.CommandHandler.Traceability
{
    public class TraceabilityFindCommandHandler : IRequestHandler<TraceabilityFindCommands, Apiresult<List<TraceabilityDtos>>>
    {
        private readonly IBaseResotity<Basic_Traceability> traceabilityRepo;
        private readonly IBaseResotity<Goods_Infomation> goodsResotity;

        public TraceabilityFindCommandHandler(IBaseResotity<Basic_Traceability> TraceabilityRepo, IBaseResotity<Goods_Infomation> GoodsResotity)
        {
            traceabilityRepo = TraceabilityRepo;
            goodsResotity = GoodsResotity;
        }
        public Task<Apiresult<List<TraceabilityDtos>>> Handle(TraceabilityFindCommands request, CancellationToken cancellationToken)
        {
            Apiresult<List<TraceabilityDtos>> res = new Apiresult<List<TraceabilityDtos>>();
            res.Code = ApiEnums.成功;
            res.Message  =ApiEnums.成功.ToString();
            var list = from a in traceabilityRepo.GetAll()
                       join b in goodsResotity.GetAll() on a.GoodsId equals b.GoodsId
                       where a.IsDelete == false && a.TraceabilityId ==request.TraceabilityId
                       select new TraceabilityDtos
                       {
                           TraceabilityId = a.TraceabilityId,
                           GoodsId = a.GoodsId,
                           GoodsName = b.GoodsName,
                           Production_Batch = a.Production_Batch,
                           Quality_Guarantee = a.Quality_Guarantee,
                           Production_License = a.Production_License,
                           Manufacturing_Enterprise = a.Manufacturing_Enterprise,
                           Information_Completeness = a.Information_Completeness,
                           NumberOfExports = a.NumberOfExports,
                           Cover_Image = a.Cover_Image,
                           CreateName = a.CreateName,
                           CreateTime = a.CreateTime,
                           UpdateName = a.UpdateName,
                       };
            res.Data = list.ToList();
            return Task.FromResult(res);
        }
    }
}
